package com.ss.android.article.browser.database.b;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.a.a.s;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.database.BrowserHistory;
import com.ss.android.common.app.AbsApplication;

import java.util.ArrayList;
import java.util.List;

@WorkerThread
public class HistoryManager extends SQLiteOpenHelper implements h {
    private static HistoryManager sInstance;
    @Nullable
    private SQLiteDatabase mSQLiteDatabase;

    private HistoryManager(@NonNull Application application) {
        super(application, "historyManager", null, 2);
    }

    public static HistoryManager getInstance() {
        if (sInstance == null) {
            synchronized (HistoryManager.class) {
                if (sInstance == null) {
                    sInstance = new HistoryManager(AbsApplication.getApplication());
                }

            }
        }

        return sInstance;
    }

    static SQLiteDatabase getDatabase(HistoryManager historyManager) {
        return historyManager.getDatabase();
    }

    static BrowserHistory a(Cursor cursor) {
        return parseBrowserHistory(cursor);
    }

    @WorkerThread
    private void insertBrowserHistory(@NonNull BrowserHistory browserHistory) {
        synchronized (this) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("url", browserHistory.url);
            contentValues.put("title", browserHistory.title);
            contentValues.put("time", Long.valueOf(System.currentTimeMillis()));
            this.getDatabase().insert("history", null, contentValues);
        }
    }

    static void a(HistoryManager historyManager, BrowserHistory browserHistory) {
        historyManager.insertBrowserHistory(browserHistory);
    }

    @NonNull
    public final com.a.a.a a(@NonNull String arg2) {
        return com.a.a.a.a(new com.ss.android.article.browser.database.b.c(this, arg2));
    }

    @NonNull
    public static BrowserHistory parseBrowserHistory(@NonNull Cursor cursor) {
        BrowserHistory browserHistory = new BrowserHistory();
        browserHistory.setUrl(cursor.getString(1));
        browserHistory.setTitle(cursor.getString(2));
        browserHistory.icon = R.drawable.icon_clock;
        browserHistory.time = cursor.getLong(cursor.getColumnIndex("time"));
        browserHistory.readableTime = BrowserHistory.convert2ReadableTime(browserHistory.time);
        return browserHistory;
    }

    @NonNull
    public final com.a.a.a b() {
        return com.a.a.a.a(new b(this));
    }

    @NonNull
    public final s b(@NonNull String keywords) {
        return s.a(new HistorySearcher(this, keywords));
    }

    @NonNull
    public final s c() {
        return s.a(new f(this));
    }

    @NonNull
    public final s d() {
        return s.a(new g(this));
    }

    @NonNull
    @WorkerThread
    final List e() {
        ArrayList v8;
        synchronized (this) {
            v8 = new ArrayList();
            Cursor cursor = this.getDatabase().query("history", null, null, null, null, null, "time DESC");
            while (cursor.moveToNext()) {
                ((List) v8).add(sInstance.parseBrowserHistory(cursor));
            }

            cursor.close();
        }
        return ((List) v8);
    }

    @NonNull
    @WorkerThread
    private SQLiteDatabase getDatabase() {
        SQLiteDatabase sqLiteDatabase;
        synchronized (this) {
            if (this.mSQLiteDatabase == null || !this.mSQLiteDatabase.isOpen()) {
                this.mSQLiteDatabase = this.getWritableDatabase();
            }
            sqLiteDatabase = this.mSQLiteDatabase;
        }
        return sqLiteDatabase;
    }

    public void onCreate(@NonNull SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE history(id INTEGER PRIMARY KEY,url TEXT,title TEXT,time INTEGER)");
    }

    public void onUpgrade(@NonNull SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS history");
        this.onCreate(sqLiteDatabase);
    }
}

